*** This performes tests for attrition

if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}

********************************************************************************
**** Are attritors different from non-attritors *******************
********************************************************************************

use "${gsdAnalysisOutput}/bscie_panel_regressions", clear

keep if wave==1

gen attrit=0
replace attrit=1 if final_sample==0
replace attrit=1 if replacement == 1 & gender == 1
label var attrit "Attrition"

clear matrix
mat A = J(37,6,.)
mat stars = J(37,6,0)

local n 1

quietly tabulate edu_level, gen(edu_level_)
quietly tabulate lit_eval, gen(lit_eval_)
quietly tabulate num_eval, gen(num_eval_)
quietly tabulate state_assigned, gen(cat_state_assigned)


foreach var in age gender married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist death_x_prox_b300 death_x_prox_a300  {


ttest `var' , by(attrit)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if  attrit == 0
local N=`r(N)'
mat A[28,1]=`N'
display `N'
sum `var' if  attrit == 1
local N=`r(N)'
mat A[28,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}

********************************************************************************

reg attrit age gender married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_b300 death_x_prox_a300

test age gender married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_b300 death_x_prox_a300


mat A[29,5] = `r(F)'
mat A[29,6] = `r(p)'
matrix stars[28,5] = (abs(A[29,5]/A[29,6]) > invttail(r(df_t),0.10/2)) + (abs(A[29,5]/A[29,6]) > invttail(r(df_t),0.05/2)) + (abs(A[29,5]/A[29,6]) > invttail(r(df_t),0.01/2))

local n 30

foreach var in cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 {

ttest `var' , by(attrit)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if  attrit == 0
local N=`r(N)'
mat A[36,1]=`N'
display `N'
sum `var' if  attrit == 1
local N=`r(N)'
mat A[36,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}


reg attrit cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6  

test cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 

mat A[37,5] = `r(F)'
mat A[37,6] = `r(p)'
matrix stars[37,5] = (abs(A[37,5]/A[37,6]) > invttail(r(df_t),0.10/2)) + (abs(A[37,5]/A[37,6]) > invttail(r(df_t),0.05/2)) + (abs(A[37,5]/A[37,6]) > invttail(r(df_t),0.01/2))


matrix rownames A ="Age" "Female" "Married"   "Business ownership" "(Log) food consumption food" ///
"Formal bank account" "Formal loans in past 5 years" "Informal loans in past 5 years" "Education level: No education"  "Education level: Primary" "Education level: Secondary" "Education level: University" ///
"Literacy: No English" "Literacy: Some English" "Literacy: Good English"  "Numeracy: Low"  "Numeracy: Counting 0-100" "Numeracy: Addition" "Numeracy: Multiplication" ///
 "Household size" "Number of children" "Number of elderly" "Number of rooms" "Number of buildings" "(Log) distance to KCB branch" ///
  "Conflict exposure (2011-2014)" "Conflict exposure (2015-2017)" "Observations" "Joint orthogonality" "Central Equatoria" ///
  "Eastern Equatoria" "Lakes" "Northern Bahr El Ghazal" "Western Bahr El Ghazal" "Western Equatoria" "Observations" "Joint orthogonality"
  frmttable , statmat(A) replace annotate(stars) asymbol(*,**,***) sdec(3) title(Table A.4 – Difference between attritors and non-attritors) ctitles( ) 
frmttable using "$gsdTables/TableA.4.tex", replay tex replace 

*****************************************************************************************************************
**** Did attritors in control group differ from attritors in treatment group ***********************
*****************************************************************************************************************

use "${gsdAnalysisOutput}/bscie_panel_regressions", clear

keep if wave==1

gen attrit=0
replace attrit=1 if final_sample==0
replace attrit=1 if replacement == 1 & gender == 1

label var attrit "Attrition"

keep if attrit==1

clear matrix
mat A = J(37,6,.)
mat stars = J(37,6,0)

local n 1
local replace replace
local append 

quietly tabulate edu_level, gen(edu_level_)
quietly tabulate lit_eval, gen(lit_eval_)
quietly tabulate num_eval, gen(num_eval_)
quietly tabulate state_old, gen(cat_state_assigned)

foreach var in age gender married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist death_x_prox_b300 death_x_prox_a300  {


ttest `var', by(treatment)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if  treatment == 0
local N=`r(N)'
mat A[28,1]=`N'
display `N'
sum `var' if  treatment == 1
local N=`r(N)'
mat A[28,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}

********************************************************************************

reg treatment age gender  married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_b300 death_x_prox_a300

test age gender married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_b300 death_x_prox_a300

mat A[29,5] = `r(F)'
mat A[29,6] = `r(p)'
matrix stars[29,5] = (abs(A[29,5]/A[29,6]) > invttail(r(df_t),0.10/2)) + (abs(A[29,5]/A[29,6]) > invttail(r(df_t),0.05/2)) + (abs(A[29,5]/A[29,6]) > invttail(r(df_t),0.01/2))

local n 30

foreach var in cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 {

ttest `var' , by(treatment)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if  treatment == 0
local N=`r(N)'
mat A[36,1]=`N'
display `N'
sum `var' if  treatment == 1
local N=`r(N)'
mat A[36,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}


reg treatment cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6  

test cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 

mat A[36,5] = `r(F)'
mat A[36,6] = `r(p)'
matrix stars[36,5] = (abs(A[36,5]/A[36,6]) > invttail(r(df_t),0.10/2)) + (abs(A[36,5]/A[36,6]) > invttail(r(df_t),0.05/2)) + (abs(A[36,5]/A[36,6]) > invttail(r(df_t),0.01/2))

matrix rownames A = "Age" "Female" "Married" "Business ownership" "(Log) food consumption food"  ///
"Formal bank account" "Formal loans in past 5 years" "Informal loans in past 5 years" "Education level: No education"  "Education level: Some primary" "Education level: Some secondary" "Education level: Some university" ///
"Literacy: No English" "Literacy: Some English" "Literacy: Good English"   "Numeracy: Low"  "Numeracy: Counting 0-100" "Numeracy: Addition" "Numeracy: Multiplication" ///
 "Household size" "Number of children" "Number of elderly" "Number of rooms" "Number of buildings" "(Log) distance to KCB branch" ///
 "Conflict exposure (2011-2014)" "Conflict exposure (2015-2017)" "Observations" "Joint orthogonality" "Central Equatoria" "Eastern Equatoria" "Lakes" "Northern Bahr El Ghazal" "Western Bahr El Ghazal" "Western Equatoria" "Observations" "Joint orthogonality"
frmttable , statmat(A) replace annotate(stars) asymbol(*,**,***) sdec(3) title(Table A.3 – Balance test between attritors from original control vs. attritors from original treatment group)
frmttable using "$gsdTables/TableA.3.tex", replay tex replace 
